using System;
using System.Collections.Generic;
using System.Text;

namespace ComputaGuru.SpEAd
{
    /// <summary>
    /// Unpacked representation of the Geometry property of a Diagram Link
    /// </summary>
    internal class DiagramLinkGeometry
    {
        private string sx = "0";
        private string sy = "0";
        private string ex = "0";
        private string ey = "0";
        private string edge = "0";
        private List<DiagramLinkLabel> linkLabels = new List<DiagramLinkLabel>();

        //properties to match the fields within a Geometry string
        internal string SX
        {
            get { return sx; }
            set { sx = value; }
        }

        public string SY
        {
            get { return sy; }
            set { sy = value; }
        }

        public string EX
        {
            get { return ex; }
            set { ex = value; }
        }

        public string EY
        {
            get { return ey; }
            set { ey = value; }
        }

        internal string Edge
        {
            get { return edge; }
            set { edge = value; }
        }

        internal List<DiagramLinkLabel> Labels
        {
            get { return linkLabels; }
            set { linkLabels = value; }
        }

        internal DiagramLinkGeometry(string geometryString)
        {
            //the geometry string is in two parts
            //the first part is the actual geometry of the actual link
            //while the second part following a $ is a collection
            //of lable geometry definitions
            string[] geometryParts = geometryString.Split('$');

            string[] geometryFields = geometryParts[0].Split(';');

            foreach (string geometryField in geometryFields)
            {
                string[] fieldParts = geometryField.Split('=');
                switch (fieldParts[0])
                {
                    case "SX":
                        this.SX = fieldParts[1];
                        break;
                    case "SY":
                        this.SY = fieldParts[1];
                        break;
                    case "EX":
                        this.EX = fieldParts[1];
                        break;
                    case "EY":
                        this.EY = fieldParts[1];
                        break;
                    case "EDGE":
                        this.Edge = fieldParts[1];
                        break;
                    default:
                        break;
                }
            }

            string[] labelFields = geometryParts[1].Split(';');
            foreach (string labelField in labelFields)
            {
                if (!String.IsNullOrEmpty(labelField))
                {
                    this.Labels.Add(new DiagramLinkLabel(labelField));
                }
            }
        }

        /// <summary>
        /// Override to transform this instance to a Geometry property string
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            string[] labelsToFind = { "LLB", "LLT", "LMT", "LMB", "LRT", "LRB", "IRHS", "ILHS" };

            StringBuilder returnString = new StringBuilder("SX=" + this.SX
                                                        + ";SY=" + this.SY
                                                        + ";EX=" + this.EX
                                                        + ";EY=" + this.EY
                                                        + ";EDGE=" + this.Edge + ";$");

            foreach (string labelToFind in labelsToFind)
            {
                DiagramLinkLabel labelMatch = this.Labels.Find(delegate(DiagramLinkLabel item)
                                                                  {
                                                                      return item.LabelName == labelToFind;
                                                                  });

                if (labelMatch != null)
                {
                    returnString.Append(labelMatch.ToString() + ";");
                }
            }

            return returnString.ToString();
        }
    }
}
